
/**
 * Project Name:cats-web
 * File Name:WebEnvironmentConfigSupport.java
 * Package Name:com.catsic.web.config.support
 * Date:2014年4月4日下午2:51:28
 * Copyright (c) 2014, oa.catsic.com All Rights Reserved.
 */
 
package com.catsic.web.config.support;


/**
 * ClassName: WebEnvironmentConfigSupport
 * Description: WebMvc环境配置，添加视图解析器
 * date: 2014年4月4日 下午2:51:28
 * author: yangyd
 */

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.repository.support.DomainClassConverter;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.data.web.SortHandlerMethodArgumentResolver;
import org.springframework.format.support.FormattingConversionService;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module;

@Configuration
public class WebEnvironmentConfigSupport extends WebMvcConfigurationSupport{
	
	@Autowired
	ServletContext servletContext;

	/**
	 * jspViewRelolver:注册jspViewRelolver,处理jsp视图,Controller控制器利用ModelView返回页面时
	 * @author yangyd
	 * @return InternalResourceViewResolver
	 */
	@Bean
	public InternalResourceViewResolver jspViewRelolver(){
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		viewResolver.setRequestContextAttribute("requestContext");
		viewResolver.setPrefix("/web/");
		viewResolver.setSuffix(".jsp");
		//viewResolver.setOrder(2);
		return viewResolver;
	}
	
	@Bean
	public InternalResourceViewResolver htmlViewRelolver(){
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		viewResolver.setRequestContextAttribute("requestContext1");
		viewResolver.setPrefix("/**");
		viewResolver.setSuffix(".html");
		//viewResolver.setOrder(2);
		return viewResolver;
	}
	
	/**
	 * 
	 * xmlViewResolver:注册XmlViewResolver，处理ireport报表视图
	 * @author zw
	 * @return
	 */
	/*@Bean
	public XmlViewResolver xmlViewResolver(){
		XmlViewResolver viewResolver = new XmlViewResolver();
		viewResolver.setLocation(new ServletContextResource(servletContext,"/WEB-INF/jasper-views.xml"));
		viewResolver.setOrder(1);
		return viewResolver;
	}*/
	/**
	 * multipartResolver:multipart解析器，提供文件上传支持
	 * @author yangyd
	 * @return
	 */
	@Bean
	public CommonsMultipartResolver multipartResolver(){
		return new CommonsMultipartResolver();
	}
	
	@Bean
	public ContentNegotiatingViewResolver viewResolver(){
		ContentNegotiatingViewResolver viewResolver =new ContentNegotiatingViewResolver(); 
		
		List<ViewResolver> viewResolvers = new ArrayList<ViewResolver>();
		viewResolvers.add(jspViewRelolver());
		viewResolvers.add(htmlViewRelolver());
		viewResolver.setViewResolvers(viewResolvers);
		return viewResolver;
	}	
	
	/**
	 * jsonView:支持application/json请求
	 * @author yangyd
	 * @return MappingJackson2JsonView
	 */
	@Bean
	public MappingJackson2JsonView jsonView(){
		return new MappingJackson2JsonView();
	}
	
	/**
	 * jacksonAnnotationIntrospector:jackson拦截器
	 * @author yangyd
	 * @return
	 */
    @Bean
    public JacksonAnnotationIntrospector jacksonAnnotationIntrospector() {
        return new JacksonAnnotationIntrospector();
    }
    
	/**
	 * objectMapper: Java对象与JSON的互换
	 * @author yangyd
	 * @return
	 */
    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        Hibernate4Module hm = new Hibernate4Module();
        /*hm.configure(Feature.FORCE_LAZY_LOADING,true);*/
        mapper.registerModule(hm);
        return mapper;
    }
    
	@Bean
	public DomainClassConverter<FormattingConversionService> domainClassConverter(){
		return new DomainClassConverter<FormattingConversionService>(mvcConversionService());
	}

	/**
	 * mappingJacksonHttpMessageConverter: json转换器
	 * @author yangyd
	 * @return
	 */
    @Bean
    public MappingJackson2HttpMessageConverter mappingJacksonHttpMessageConverter() {
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        messageConverter.setObjectMapper(objectMapper());
		List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
		supportedMediaTypes.add(MediaType.TEXT_HTML);
        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
		messageConverter.setSupportedMediaTypes(supportedMediaTypes);
        return messageConverter;
    }
    
    /**
	 * configureMessageConverters：添加json转换器
	 **
	 */
	@Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = mappingJacksonHttpMessageConverter();
		converters.add(mappingJackson2HttpMessageConverter);
    }
	
	/**
	 * 添加方法参数解析器
	 * PageableHandlerMethodArgumentResolver，SortHandlerMethodArgumentResolver 使得Pageable跟Sort能作为控制层的参数使用
	 * @see org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#addArgumentResolvers(java.util.List)
	 */
	@Override
	public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
		argumentResolvers.add(new PageableHandlerMethodArgumentResolver());
		argumentResolvers.add(new SortHandlerMethodArgumentResolver());
	}

	/**
	 * ResourceHandlerRegistry 完成获取静态资源的责任
	 * @see org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport#addResourceHandlers(org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry)
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/images/**").addResourceLocations("/images/");
		registry.addResourceHandler("/scripts/**").addResourceLocations("/scripts/");
		registry.addResourceHandler("/css/**").addResourceLocations("/css/");
		registry.addResourceHandler("/web/**").addResourceLocations("/web/");
		registry.addResourceHandler("/mobile/**").addResourceLocations("/mobile/");
		registry.addResourceHandler("/wechat/**").addResourceLocations("/wechat/");
		registry.addResourceHandler("/site/**").addResourceLocations("/site/");
		registry.addResourceHandler("/ipad/**").addResourceLocations("/ipad/");
	}
	
	/*    @Bean(name = "marshaller")
    public Jaxb2Marshaller jaxb2Marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath("com.x);
        return marshaller;
    }

    @Bean
    public MarshallingHttpMessageConverter marshallingMessageConverter() {
        return new MarshallingHttpMessageConverter(
                jaxb2Marshaller(),
                jaxb2Marshaller()
        );
    }*/
}
